<?php


namespace app\admin\controller;

use app\admin\model\LeaveConfig;
use app\admin\model\LeaveUserCancel;
use app\admin\model\WechatUser;

/**
 * 请假管理
 * Class Leave
 * @package app\admin\controller
 */
class Leave extends Admin
{
    /**
     * 假别管理
     */
    public function leave_config()
    {
        $map = array(
            'status' => 1,
        );
        $search = input('search');
        //搜索
        if ($search != '') {
            $map['name'] = ['like', '%' . $search . '%'];
        }
        $list = LeaveConfig::where($map)->paginate(10);
        $this->assign('list',$list);

        return $this->fetch();
    }

    public function leave_config_add()
    {
        $Model = new LeaveConfig();
        if(IS_POST) {
            $params = input('post.');
            $name = isset($params['name']) && !empty($params['name']) ? $params['name'] : null;
            $totalDays = isset($params['total_days']) && !empty($params['total_days']) ? $params['total_days'] : null;
            $is_show_total_days = isset($params['is_show_total_days']) && !empty($params['is_show_total_days']) ? $params['is_show_total_days'] : 0;
            $remark = isset($params['remark']) && !empty($params['remark']) ? $params['remark'] : '';
            if(empty($name) || empty($totalDays)) {
                return $this->error("假别和天数不能为空");
            }
            if($totalDays < 0) {
                return $this->error("假期天数必须大于0");
            }
            $data = [];
            $data['name'] = $name;
            $data['total_days'] = $totalDays;
            $data['is_show_total_days'] = $is_show_total_days;
            $data['remark'] = $remark;
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            $info = $Model->save($data);
            if($info) {
                return $this->success("新增成功",Url('Leave/leave_config'));
            }else{
                return $this->error($Model->getError());
            }
        }else{
            $this->assign('msg','');

            return $this->fetch('leave_config_edit');
        }
    }

    public function leave_config_edit()
    {
        $Model = new LeaveConfig();
        if(IS_POST) {
            $params = input('post.');
            $id = isset($params['id']) && !empty($params['id']) ? $params['id'] : null;
            $name = isset($params['name']) && !empty($params['name']) ? $params['name'] : null;
            $totalDays = isset($params['total_days']) && !empty($params['total_days']) ? $params['total_days'] : null;
            $is_show_total_days = isset($params['is_show_total_days']) && !empty($params['is_show_total_days']) ? $params['is_show_total_days'] : 0;
            $remark = isset($params['remark']) && !empty($params['remark']) ? $params['remark'] : '';
            if(empty($name) || empty($totalDays)) {
                return $this->error("参数缺失");
            }
            if($totalDays < 0) {
                return $this->error("假期天数必须大于0");
            }
            $data = [];
            $data['name'] = $name;
            $data['total_days'] = $totalDays;
            $data['is_show_total_days'] = $is_show_total_days;
            $data['remark'] = $remark;
            $info = $Model->save($data,['id'=>$id]);
            if($info){
                return $this->success("修改成功",Url("Leave/leave_config"));
            }else{
                return $this->get_update_error_msg($Model->getError());
            }
        }else{
            $id = input('id');
            $msg = $Model->get($id);
            $this->assign('msg',$msg);

            return $this->fetch();
        }
    }

    public function leave_config_del()
    {
        $id = input('id');
        $data['status'] = '-1';
        $info = LeaveConfig::where('id',$id)->update($data);
        if($info) {
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }

    /**
     * 假别取消管理
     */
    public function user_cancel()
    {
        $this->updateExpiredData();
        $map = array(
            'status' => ['egt',0],
        );
        $search = input('search');
        //搜索
        if ($search != '') {
            $map['name'] = ['like', '%' . $search . '%'];
        }
        $list = LeaveUserCancel::where($map)->order('id desc')->paginate(10);
        foreach ($list as $key => $value) {
            $list[$key]['leave_name'] = LeaveConfig::where('id',$value['leave_id'])->value('name');
            $list[$key]['user_name'] = WechatUser::where('userid',$value['userid'])->value('name');
            $list[$key]['effect_time'] = date("Y-m-d",strtotime($value['start_time']))." 至 ".date("Y-m-d",strtotime($value['end_time']));
        }
        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * 更新失效数据
     */
    private function updateExpiredData()
    {
        LeaveUserCancel::where('status',1)->where('end_time','lt',date("Y-m-d H:i:s"))->update(['status' => 0]);
    }

    public function user_cancel_add()
    {
        $Model = new LeaveUserCancel();
        if(IS_POST) {
            $params = input('post.');
            $leaveId = isset($params['leave_id']) && !empty($params['leave_id']) ? $params['leave_id'] : null;
            $userId = isset($params['userid']) && !empty($params['userid']) ? $params['userid'] : null;
            $startTime = isset($params['start_time']) && !empty($params['start_time']) ? $params['start_time'] : null;
            $endTime = isset($params['end_time']) && !empty($params['end_time']) ? $params['end_time'] : null;
            $reason = isset($params['reason']) && !empty($params['reason']) ? $params['reason'] : null;
            if(empty($leaveId) || empty($userId) || empty($startTime) || empty($endTime) || empty($reason)) {
                return $this->error("所有内容必填不能为空，请检查");
            }
            $isExist = LeaveUserCancel::where('userid',$userId)
                ->where('leave_id',$leaveId)
                ->where('status',1)
                ->value('id');
            if($isExist > 0) {
                return $this->error("该用户对应假别已取消，请勿重复操作");
            }
            $startTime = date("Y-m-d 00:00:00",strtotime($startTime));
            $endTime = date("Y-m-d 23:59:59",strtotime($endTime));
            $data = [];
            $data['leave_id'] = $leaveId;
            $data['userid'] = $userId;
            $data['reason'] = $reason;
            $data['start_time'] = $startTime;
            $data['end_time'] = $endTime;
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            $info = $Model->save($data);
            if($info) {
                return $this->success("新增成功",Url('Leave/user_cancel'));
            }else{
                return $this->error($Model->getError());
            }
        }else{
            $this->assign('msg','');
            $leaveList = LeaveConfig::where('status',1)->field('id,name')->select();
            $this->assign('leaveList',$leaveList);
            $userList = WechatUser::where('status','egt',0)->where('department','not in',[34,39])->field('userid,name as user_name')->select();
            $this->assign('userList',$userList);
            $startTime = date("Y-m-d");
            $this->assign('startTime',$startTime);
            $endTime = date("Y-12-31");
            $this->assign('endTime',$endTime);

            return $this->fetch('user_cancel_edit');
        }
    }

    public function user_cancel_edit()
    {
        $Model = new LeaveUserCancel();
        if(IS_POST) {
            $params = input('post.');
            $id = isset($params['id']) && !empty($params['id']) ? $params['id'] : null;
            $leaveId = isset($params['leave_id']) && !empty($params['leave_id']) ? $params['leave_id'] : null;
            $userId = isset($params['userid']) && !empty($params['userid']) ? $params['userid'] : null;
            $startTime = isset($params['start_time']) && !empty($params['start_time']) ? $params['start_time'] : null;
            $endTime = isset($params['end_time']) && !empty($params['end_time']) ? $params['end_time'] : null;
            $reason = isset($params['reason']) && !empty($params['reason']) ? $params['reason'] : null;
            if(empty($leaveId) || empty($userId) || empty($startTime) || empty($endTime) || empty($reason)) {
                return $this->error("所有内容必填不能为空，请检查");
            }
            $isExist = LeaveUserCancel::where('userid',$userId)
                ->where('leave_id',$leaveId)
                ->where('id','neq',$id)
                ->where('status',1)
                ->value('id');
            if($isExist > 0) {
                return $this->error("该用户对应假别已取消，请勿重复操作");
            }
            $startTime = date("Y-m-d 00:00:00",strtotime($startTime));
            $endTime = date("Y-m-d 23:59:59",strtotime($endTime));
            $data = [];
            $data['leave_id'] = $leaveId;
            $data['userid'] = $userId;
            $data['reason'] = $reason;
            $data['start_time'] = $startTime;
            $data['end_time'] = $endTime;
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            $info = $Model->save($data,['id'=>$id]);
            if($info){
                return $this->success("修改成功",Url("Leave/user_cancel"));
            }else{
                return $this->get_update_error_msg($Model->getError());
            }
        }else{
            $id = input('id');
            $msg = $Model->get($id);
            $msg['start_time'] = date("Y-m-d",strtotime($msg['start_time']));
            $msg['end_time'] = date("Y-m-d",strtotime($msg['end_time']));
            $this->assign('msg',$msg);
            $leaveList = LeaveConfig::where('status',1)->field('id,name')->select();
            $this->assign('leaveList',$leaveList);
            $userList = WechatUser::where('status','egt',0)->where('department','not in',[34,39])->field('userid,name as user_name')->select();
            $this->assign('userList',$userList);
            $startTime = date("Y-m-d");
            $this->assign('startTime',$startTime);
            $endTime = date("Y-12-31");
            $this->assign('endTime',$endTime);

            return $this->fetch();
        }
    }

    public function user_cancel_del()
    {
        $id = input('id');
        $data['status'] = '-1';
        $info = LeaveUserCancel::where('id',$id)->update($data);
        if($info) {
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }

    /**
     * 批量删除
     */
    public function cancelMoveToTrash()
    {
        $ids = input('ids/a');
        if (!$ids) {
            return $this->error('请勾选删除选项');
        }
        $data['status'] = -1;
        $info = LeaveUserCancel::where('id', 'in', $ids)->update($data);

        if ($info) {
            return $this->success('批量删除成功', url('Leave/user_cancel'));
        } else {
            return $this->error('批量删除失败');
        }
    }

}